Imports System.Collections.Generic

Partial Class Admin_Permissions
    Inherits System.Web.UI.Page

    Public Function PermissionIDList() As System.Collections.Generic.List(Of Cdm.DataCore.UserSecurity.Permission)
        Return Cdm.DataCore.UserSecurity.Permission.ListAll()
    End Function

    Public Function DisplayPermission(ByVal permID As Integer) As String

        Dim i As Integer
        For i = 0 To PermissionIDList().Count - 1
            If PermissionIDList(i).PermissionId = permID Then
                Return PermissionIDList(i).Name
            End If
        Next

        Return ""

    End Function

    Protected Sub SaveButton_Click(ByVal sender As Object, _
                                ByVal e As System.EventArgs) _
                                Handles SaveButton.Click

        Dim reqField As Cdm.DataCore.UserSecurity.RolePermission
        For Each row As GridViewRow In Me.PermissionsScrollingGridView.Rows

            Dim chkBoxView As CheckBox
            Dim chkBoxUpdate As CheckBox
            Dim chkBoxInsert As CheckBox

            If row.RowType = DataControlRowType.DataRow Then

                chkBoxView = row.Cells(1).FindControl("ViewCheckBox")
                chkBoxUpdate = row.Cells(2).FindControl("UpdateCheckBox")
                chkBoxInsert = row.Cells(3).FindControl("InsertCheckBox")

                reqField = Cdm.DataCore.UserSecurity.RolePermission.Retrieve(Me.PermissionsScrollingGridView.DataKeys(row.RowIndex).Values(0))
                reqField.MayView = chkBoxView.Checked
                reqField.MayUpdate = chkBoxUpdate.Checked
                reqField.MayInsert = chkBoxInsert.Checked
                reqField.Persist()

            End If

        Next

        Me.RolePermissionsObjectDataSource.Select()
        Me.PermissionsScrollingGridView.DataBind()

        SetStatus("Permissions Saved")

    End Sub

    Protected Sub CheckSelectAllCheckBox()

        Dim selectHasViewHeader As Boolean = True
        Dim selectHasUpdateHeader As Boolean = True
        Dim selectHasInsertHeader As Boolean = True

        Dim hasPermissionCheckBox As CheckBox
        Dim selectAllCheckBox As CheckBox

        Dim permissionName As String
        
        For Each row As GridViewRow In Me.PermissionsScrollingGridView.Rows

            permissionName = CType(row.Cells(0).Controls(0), DataBoundLiteralControl).Text.Trim()

            If row.RowType = DataControlRowType.DataRow Then

                hasPermissionCheckBox = CType(row.Cells(1).FindControl("ViewCheckBox"), CheckBox)
                If hasPermissionCheckBox IsNot Nothing Then
                    If Not hasPermissionCheckBox.Checked Then
                        selectHasViewHeader = False
                    End If
                    hasPermissionCheckBox.InputAttributes.Add("title", "Read " & permissionName)
                End If

                hasPermissionCheckBox = CType(row.Cells(2).FindControl("UpdateCheckBox"), CheckBox)
                If hasPermissionCheckBox IsNot Nothing Then
                    If Not hasPermissionCheckBox.Checked Then
                        selectHasUpdateHeader = False
                    End If
                    hasPermissionCheckBox.InputAttributes.Add("title", "Change " & permissionName)
                End If

                hasPermissionCheckBox = CType(row.Cells(3).FindControl("InsertCheckBox"), CheckBox)
                If hasPermissionCheckBox IsNot Nothing Then
                    If Not hasPermissionCheckBox.Checked Then
                        selectHasInsertHeader = False
                    End If
                    hasPermissionCheckBox.InputAttributes.Add("title", "Add " & permissionName)
                End If

                If Not selectHasViewHeader And Not selectHasUpdateHeader And Not selectHasInsertHeader Then
                    Exit For
                End If

            End If

        Next

        selectAllCheckBox = CType(Me.PermissionsScrollingGridView.HeaderRow.Cells(1).FindControl("ViewSelectAllCheckBox"), CheckBox)
        If selectAllCheckBox IsNot Nothing Then
            selectAllCheckBox.Checked = selectHasViewHeader
            selectAllCheckBox.InputAttributes.Add("title", "Select or de-select all Read permissions")
        End If

        selectAllCheckBox = CType(Me.PermissionsScrollingGridView.HeaderRow.Cells(2).FindControl("UpdateSelectAllCheckBox"), CheckBox)
        If selectAllCheckBox IsNot Nothing Then
            selectAllCheckBox.Checked = selectHasUpdateHeader
            selectAllCheckBox.InputAttributes.Add("title", "Select or de-select all Change permissions")
        End If

        selectAllCheckBox = CType(Me.PermissionsScrollingGridView.HeaderRow.Cells(3).FindControl("InsertSelectAllCheckBox"), CheckBox)
        If selectAllCheckBox IsNot Nothing Then
            selectAllCheckBox.Checked = selectHasInsertHeader
            selectAllCheckBox.InputAttributes.Add("title", "Select or de-select all Add permissions")
        End If

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetupJavaScript()
        ClearStatus()
        If Not Page.IsPostBack Then
            Me.PermissionsScrollingGridView.Sort("PermissionId", SortDirection.Ascending)
        End If

    End Sub

    Private Sub ClearStatus()
        SetStatus("")
        
    End Sub
    Private Sub SetStatus(ByVal message As String)
        Dim mymaster As ASP.admin_admin_master = CType(Me.Master, ASP.admin_admin_master)
        mymaster.SetStatus(message)
    End Sub

    Private Sub SetupJavaScript()

        EntityController.JavaScriptAddChildDiv(Me.PermissionsScrollingGridView.DivClientId, CInt(Me.PermissionsScrollingGridView.ToolTip))
        ClientScript.RegisterStartupScript(Me.GetType(), "scrollDivs", EntityController.JavaScriptScrollingGridStartup(Me.PermissionsScrollingGridView), True)
        EntityController.JavaScriptSetFixedHeight(28)
        ScriptManager.RegisterClientScriptBlock(Me.PermissionsScrollingGridView, Me.PermissionsScrollingGridView.GetType(), _
        "scrollDivAsync", EntityController.JavaScriptScrollingGridStartup(Me.PermissionsScrollingGridView), True)
    End Sub

    Protected Sub RolesDropDownList_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles RolesDropDownList.DataBound
        SetDescription()
    End Sub

    Protected Sub RolesDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RolesDropDownList.SelectedIndexChanged
        SetDescription()
    End Sub


    Private Sub SetDescription()
        Me.PermissionsScrollingGridView.Caption = "Establish type of access to Participant file for each Role. (Current Role: " & Me.RolesDropDownList.SelectedItem.Text & ")"
    End Sub

    Protected Sub PermissionsScrollingGridView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles PermissionsScrollingGridView.DataBound
        If Me.PermissionsScrollingGridView.Rows.Count > 0 Then
            CheckSelectAllCheckBox()
        End If
        AccessibilityController.AddImageToSortableGridHeaders(PermissionsScrollingGridView)
    End Sub

    Protected Sub ResetButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ResetButton.Click
        Me.RolePermissionsObjectDataSource.Select()
        PermissionsScrollingGridView.DataBind()
        SetStatus("Form has been reset.")
    End Sub
End Class
